<?php

namespace App\Models;


use Illuminate\Database\Eloquent\Model;

/**
 * App\Models\Base
 *
 * @method static \Illuminate\Database\Eloquent\Builder|Base newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|Base newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|Base query()
 * @mixin \Eloquent
 */
class Base extends Model
{
    private $formatTree = [];

    /**
     * 状态转换
     * @param $data
     * @param array $map
     * @return mixed
     */
    protected function stateToText(&$data, $map = []){
        foreach ($data as $key => &$row) {
            foreach ($map as $col => $pair) {
                if (isset($row[$col]) && isset($pair[$row[$col]])) {
                    $text = $col . '_text';
                    $row[$text] = $pair[$row[$col]];
                }
            }
        }
        return $data;
    }

    /**
     * 把返回的数据集转换成Tree
     * @param $list
     * @param string $pk
     * @param string $pid
     * @param string $child
     * @param int $root
     * @return array
     */
    public function listToTree($list, $pk='id', $pid='pid', $child='children', $root=0){
        // 创建Tree
        $tree = [];
        // 创建基于主键的数组引用
        $refer = [];
        foreach ($list as $key => $data) {
            $refer[$data[$pk]] =& $list[$key];
        }
        foreach ($list as $key => $data) {
            // 判断是否存在parent
            $parentId = $data[$pid];
            if ($root == $parentId) {
                $tree[] =& $list[$key];
            } else {
                if (isset($refer[$parentId])) {
                    $parent =& $refer[$parentId];
                    $parent[$child][] =& $list[$key];
                }
            }
        }
        return $tree;
    }

    /**
     * 获取上一篇
     * @param $id
     * @return mixed
     */
    protected function prev($id){
        return $this->select(['id','title'])->where('status',1)->where('id', '>', $id)->first();
    }

    /**
     * 获取下一篇
     * @param $id
     * @return mixed
     */
    protected function next($id){
        return $this->select(['id','title'])->where('status',1)->where('id', '<', $id)->first();
    }


    /**
     * 将格式数组转换为树
     * @param $list
     * @param string $title
     * @param string $pk
     * @param string $pid
     * @param int $root
     * @return mixed
     */
    protected function toFormatTree($list, $title = 'title', $pk = 'id', $pid = 'pid', $root = 0)
    {
        $list = $this->listToTree($list,$pk,$pid,'_child',$root);
        $this->_toFormatTree($list,0,$title);
        return $this->formatTree;
    }

    /**
     * 将格式数组转换为树
     * @param $list
     * @param int $level
     * @param string $title
     */
    protected function _toFormatTree($list, $level = 0, $title = 'title')
    {
        foreach($list as $key=>$val){
            $tmp_str = '';
            $tmp_str.= "　∟";//空格搜狗输入法v+1+d
            $val['level'] = $level;
            $val['title_show'] = $level==0 ? $val[$title] : $tmp_str.$val[$title];

            if(!array_key_exists('_child',$val)){
                array_push($this->formatTree,$val);
            }else{
                $tmp_ary = $val['_child'];
                unset($val['_child']);

                array_push($this->formatTree,$val);
                $this->_toFormatTree($tmp_ary,$level+1,$title); //进行下一层递归
            }
        }
        return;
    }



}
